﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data.OleDb;
using System.IO;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using U8SrvTrans;
using System.Configuration;
using System.Dynamic;
using U8;

namespace WebApplication1
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class postGX : IHttpHandler
    {
        public static readonly string bz = ConfigurationManager.ConnectionStrings["conn1"].ConnectionString;

        public void ProcessRequest(HttpContext context)
        {
            string authHeader = HttpContext.Current.Request.Headers["Authorization"];
            string bearerToken = authHeader.Substring("Bearer ".Length).Trim();
            PetaPoco.Database dbw = new PetaPoco.Database(bz);
            XZ_RZBfaModel RZB = new XZ_RZBfaModel();
            RZB.类型 = "工艺";
            RZB.日期 = DateTime.Now;

            object jsonData = "";
            FileLogHelper.WriteTxtLog("调用开始", "11");
            dynamic jsonF = new ExpandoObject();  //动态MODEL 存DATE
            dynamic res = new ExpandoObject();  //动态MODEL 返回值
            res.msg = "";
            TokenModel model = dbw.FirstOrDefault<TokenModel>("select  * from  Token  where token=@token and  DATEDIFF(DAY,date, GETDATE())=0", new { token = bearerToken });
            if (model == null)
            {
                res.code = 401;
                res.msg = "密钥不合法或者已经过期";
                res.data = "XXXX";
                res.count = 0;
                jsonData = JsonConvert.SerializeObject(res);
                context.Response.Write(jsonData);
            }
            else
            {
                try
                {

                    byte[] postData = new byte[context.Request.InputStream.Length];
                    context.Request.InputStream.Read(postData, 0, postData.Length);
                    string postDataString = System.Text.Encoding.UTF8.GetString(postData);
                    FileLogHelper.WriteTxtLog("调用开始", postDataString);

                    RZB.JSON = postDataString;

                    InventoryInfo_GX U8fa = JsonConvert.DeserializeObject<InventoryInfo_GX>(postDataString);
                    RZB.返回ID = U8fa.OpCode;
                    if (string.IsNullOrEmpty(U8fa.Description) || string.IsNullOrEmpty(U8fa.OpCode))
                    {
                        res.code = 999;
                        res.msg = "传入值有问题，请检查";
                        RZB.是否成功 = "失败";
                        RZB.错误内容 = "传入值有问题，请检查";
                        return;
                    }


                    Inventory_gx aa = new Inventory_gx();

                    //修改工序，如果返回有变量改动，则修改i完毕。否则增加
                    int aaa = dbw.Execute("update  sfc_operation  set  Description='" + U8fa.Description + "'  where  OpCode='" + U8fa.OpCode + "'");
                    if (aaa != 0)
                    {
                        res.msg = "更新成功";
                        res.code = 200;
                        RZB.是否成功 = "成功";
                        return;

                    }

                    res.msg = aa.Save(U8fa);
                    if (res.msg != "")
                    {
                        RZB.是否成功 = "失败";
                        RZB.错误内容 = res.msg;
                        res.code = 999;
                        return;
                    }

                    res.code = 200;
                    res.msg = "成功";
                }
                catch (Exception ee)
                {
                    RZB.是否成功 = "True";
                    RZB.错误内容 = ee.Message; ;
                    RZB.返回ID = "";

                    if (ee.Message.Length > 50)
                    {
                        res.msg = ee.Message.Substring(0, 50);
                    }
                    else
                    {
                        res.msg = ee.Message;
                    }
                    res.code = 999;
                    


                }
                finally
                {

                    dbw.Save(RZB);
                    jsonData = JsonConvert.SerializeObject(res);
                    context.Response.ContentType = "application/json";
                    context.Response.Write(jsonData);
                }
                
            }
          
        }




        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public string res(string ccode, string msg, string jsonData)
        {
            string resstr = "";


            return resstr;
        }


    }
}